﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Animation;
using PairingMatrix.Common.Enum;
using PairingMatrix.Model;
using PairingMatrix.ViewModel;
using System.Linq;

namespace PairingMatrix.View.Behavior
{
    public class ArrangeAbsenteesControlBehavior : Behavior<AbsenteesBoxControl>
    {
        private AbsenteesViewModel _absenteesViewModel;
        private Storyboard _storyboard;
        private Storyboard _storyboardTranslate;
//        private DoubleAnimation _scaleDownAnimation;
        private DoubleAnimation _rotateAnimation;
        private DoubleAnimation _translateAnimation;

        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (sender, e) =>
            {
                _absenteesViewModel = (AbsenteesViewModel)AssociatedObject.DataContext;
                _absenteesViewModel.PropertyChanged += Absentees_PropertyChanged;

                TimeSpan duration = TimeSpan.FromMilliseconds(500);
                _storyboard = new Storyboard {Duration = duration};

//                _scaleDownAnimation = new DoubleAnimation();
//                _storyboard.Children.Add(_scaleDownAnimation);
//                _scaleDownAnimation.Duration = duration;
//                Storyboard.SetTarget(_scaleDownAnimation, AssociatedObject);
//                Storyboard.SetTargetProperty(_scaleDownAnimation, new PropertyPath("Height"));


                _rotateAnimation = new DoubleAnimation();
                _storyboard.Children.Add(_rotateAnimation);
                _rotateAnimation.Duration = duration;
                Storyboard.SetTarget(_rotateAnimation, AssociatedObject.rotateTransform);
                Storyboard.SetTargetProperty(_rotateAnimation, new PropertyPath("Angle"));

                _storyboard.Completed += _storyboard_Completed;



                _storyboardTranslate = new Storyboard { Duration = duration };

                _translateAnimation = new DoubleAnimation();
                _storyboardTranslate.Children.Add(_translateAnimation);
                _translateAnimation.Duration = duration;
                Storyboard.SetTarget(_translateAnimation, AssociatedObject.translateTransform);
                Storyboard.SetTargetProperty(_translateAnimation, new PropertyPath("Y"));

                _storyboardTranslate.Completed += _storyboardTranslate_Completed;
                _absenteesViewModel.AbsentTeamMembers.ToList().ForEach(a => a.ShowCardInAbsenteesBox = true);

                _absenteesViewModel. ShowAbsenteesBox = true;

            };

        }

        void Absentees_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ShowAbsenteesBox")
            {
                if (_absenteesViewModel.ShowAbsenteesBox)
                    ShowBox();
                else
                    HideBox();
            }
        }

        private bool showBox = false;
        private void ShowBox()
        {
            showBox = true;
            _translateAnimation.To = 0;
            _storyboardTranslate.Begin();
        }

        private void HideBox()
        {
            showBox = false;
            _absenteesViewModel.AbsentTeamMembers.ToList().ForEach(a => a.ShowCardInAbsenteesBox = false);
//            _scaleDownAnimation.To = 40;
            _rotateAnimation.To = 90;
            _storyboard.Begin();
        }

        void _storyboard_Completed(object sender, EventArgs e)
        {
            if(!showBox)
            {
                _translateAnimation.To = 200;
                _storyboardTranslate.Begin();
            }
            else
            {
                AssociatedObject.collapseImageRotate.Angle = 0;
            }
        }

        void _storyboardTranslate_Completed(object sender, EventArgs e)
        {
            if (showBox)
            {
                _absenteesViewModel.AbsentTeamMembers.ToList().ForEach(a => a.ShowCardInAbsenteesBox = true);
//                _scaleDownAnimation.To = 500;
                _rotateAnimation.To = 0;
                _storyboard.Begin();
            }
            else
            {
                AssociatedObject.collapseImageRotate.Angle = 90;
            }
        }


    }
}
